<!-- 打包操作扫描视图 -->
<style type="text/css">
.modal .modal-dialog {
	margin-top: 180px;
}
.sort-message .form-inline {
	margin-bottom: 10px;
}
#countStock {
	margin-top: 10px;
}
#countStock p{
	margin-right:10px;
}
#scanForm .form-group {
	margin-right: 1px;
}
#addForm .t-detail input {
	width: 131.3px;	
}
#addForm .t-detail, #addForm .textarea-memo{
	margin-top: 5px;
	margin-bottom: 0;
}
a {
	cursor: pointer;
}
</style>
<script src="/include/bootstrapadmin/js/select2.min.js"></script>
<div class="pageheader">
	<h2>
		<i class="fa fa-pencil-square"></i>日志管理
	</h2>
	<div class="breadcrumb-wrapper">
		<span class="label">你所在的位置：</span>
		<ol class="breadcrumb">
			<li><a href="/index">ERP管理系统</a></li>
			<li>日志管理</li>
			<li>网站订单日志</li>
			<li class="active">打包|操作中心扫描</li>
		</ol>
	</div>
</div>
<div class="contentpanel">
<div class="main hidden">
    <form id="scanForm" class="form-inline">
        <div class="form-group">
            <input placeholder="扫描或输入条形码" type="text" name="barcode" value="<?php echo $this->barcode;?>" class="form-control" name="title">
        </div>
        <button type="submit" class="btn btn-primary">确定</button> 
    </form>
    <form id="addForm" method="post" action="/log/pack">
        <div class="table-responsive">
            <div>
                <label>订单信息：</label>
                <input type="text" hidden="hidden" value="<?php echo $this->orderid;?>" name="orderid">
                <input type="text" hidden="hidden" value="<?php echo $this->site;?>" name="site">
            </div>
            <table class="table order-info">
                <thead>
                    <tr class="info">
                        <th>订单号</th>
                        <th>SKU</th>
                        <th>产品名称</th>
                        <th>尺码和数量</th>
                        <th>网站</th>
                        <th>下单时间</th>
                        <th>状态</th>
                    </tr>
                </thead>
                <tbody>
                <?php if ($this->loglist):?>
                <?php foreach ($this->loglist as $v):?>
                <tr class="info">
				    <td><a target="_blank" href="<?php echo $this->url(array('controller'=>'log','action'=>'index','oid' =>$v['order_id'],'site'=>$v['site']),null,true); ?>"><?php echo $v['order_id']; ?></a></td>
                    <td><?php echo $v['sku'];?></td>
                    <td><?php echo $v['title'];?></td>
                    <td>
                        <?php 
                        $info = json_decode($v['info'], true);
                        $vinfo = '';
                        //处理$v['info']可能出现重复的尺码的问题
                        foreach($info as $v1){
                        	$vnum = 0;
                        	foreach ($info as $v2){
                        		if($v1[0] == $v2[0]){
                        			$vnum += $v2[1];
                        		}
                        	}
                        	$flag = true;
                        	foreach ($vinfo as $v3){
                        		if($v1[0] == $v3[0]){
                        			$flag = false;
                        		}
                        	}
                        	if($flag){
                        		$vinfo[] = array($v1[0],$vnum);
                        	}
                        }
                        foreach($vinfo as $_v){
                        	$act = '';
                        	if ($v['info_stock'] == 'add') {
                        		$act = '+';	
                        	} else {
                        		$act = '-';	
                        	}
                        	echo '尺码: ' . $_v[0] . ', 库存: ' . $act . stripslashes($_v[1]) . '<br/>';
                        }?>
				    </td>
				    <td><?php echo $v['site'];?></td>
				    <td><?php echo $v['add_time']; ?></td>
				    <td>
				        <?php
				        switch ($v['statu'])
				        {
				        	case 1:
				        		echo '<span class="label label-danger">分拣中、缺货中</span>';
				        		break;
				        	case 2:
				        		echo '<span class="label label-info">分拣完、未处理</span>';
				        		break;
				        	case 3:
				        		echo '<span class="label label-primary">分拣完、处理中</span>';
				        		break;
				        	case 4:
				        		echo '<span class="label label-success">分拣完、处理完</span>';
				        		break;
				        	default:
				        		echo '<span class="label label-warning">未分拣、未处理</span>';
				        }
				        ?>
				    </td>
			    </tr>
			    <?php endforeach;?> 
			    <?php endif;?>
                </tbody>
            </table>
		</div>
		<div class="table-responsive sort-message">
            <div class="form-inline">
                <label>分拣信息：</label>
                <div class="form-group" role="alert">
                                        扫描完成后，系统会自动匹配分拣信息：若完全匹配，则订单显示<span class="label label-success">绿色</span>；若不匹配，则扫描项显示<span class="label label-danger">红色</span>
                </div> 
            </div>
            <table class="table table-bordered sort-info">
                <thead>
                    <tr>
                        <th>SKU</th>
                        <th>产品名称</th>
                        <th>尺码和数量</th>
                        <th>是否赠品</th>
                        <th>分拣员</th>
                        <th>分拣时间</th>
                        <th>状态</th>
                        <th>删除</th>
                    </tr>
                </thead>
                <tbody>
                <?php if ($this->sortloglist):?>
                <?php foreach ($this->sortloglist as $v):?>
                <tr class="<?php echo $v['sku'].$v['size'];?>">
                    <td><?php echo $v['sku'];?><input type="text" hidden="hidden" value="<?php echo $v['sku'];?>" name="sort_sku[]"></td>
                    <td><?php echo $v['title'];?><input type="text" hidden="hidden" value="<?php echo $v['id'];?>" name="sort_id[]"></td>
                    <td>
                        <?php echo '尺码: ' . $v['size'] . ', 数量: 1 <br/>'; ?>
                        <input type="text" hidden="hidden" value="<?php echo $v['size'];?>" name="o_size[<?php echo $v['sku'];?>][]">
                        <input class="size_num_<?php echo $v['size'];?>" name="onum_<?php echo $v['sku'];?>_<?php echo $v['size'];?>" type="text" hidden="hidden" value="1">
                        <input class="check_num_<?php echo $v['size'];?>" name="cnum_<?php echo $v['sku'];?>_<?php echo $v['size'];?>" hidden="hidden" type="text" value="0">
				    </td>
				    <td><?php if($v['isgift']){echo '是';}else{echo '否';}?></td>
				    <td><?php echo $v['adduser']; ?></td>
				    <td><?php echo $v['add_time']; ?></td>
				    <td>
				        <?php
				        switch ($v['statu'])
				        {
				        	case 1:
				        		echo '<span class="status label label-primary">已操作、未打包</span>';
				        		break;
				        	case 2:
				        		echo '<span class="status label label-success">已打包</span>';
				        		break;
				        	default:
				        		echo '<span class="status label label-warning">未处理</span>';
				        }
				        ?>
				    </td>
				    <td><a href="javascript:void(0);" onclick="deleteSort(<?php echo $v['id']; ?>, this)">删除</a></td>
			    </tr>
			    <?php endforeach;?> 
			    <?php endif;?>
                </tbody>
            </table>
		</div>
        <div class="table-responsive">
            <div><label>扫描信息：</label></div>
            <table id="page-scan-info" class="table table-bordered">
                <thead>
                    <tr>
                        <th><input type="checkbox" name="checkall">&nbsp;&nbsp;全选</th>
                        <th>SKU</th>
                        <th>产品名称</th>
                        <th>尺码和数量</th>
                        <th>成本价</th>
                        <th>市场价</th>
                        <th class="oper-pack-time">打包时间</th>
                        <th>操作员</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
        <div class="form-inline">
            <input id="billid" type="text" name="billid" class="form-control inline" placeholder="请输入快递单号">
            <input id="oper-or-pack" class="btn btn-primary form-control" type="submit" value="进行打包"> 
            <input id="pack-memo" class="btn btn-primary form-control" type="button" value="选择印号"> 
            <input class="btn btn-default form-control" type="button" value="删除" onclick="delscanlog();">
        </div>
        <div class="form-group textarea-memo hidden">
            <div class="col-sm-5" style="padding-left: 0;width: 412px;">
                <select class="select2" multiple="multiple" name="memo[]" data-placeholder="选择印号...">
                    <option value=""></option>
                    <?php 
                    foreach ($this->loglist as $lv){ 
            	        if(!empty($lv['print_msg'])){
            		        $str = $lv['print_msg'];
            		        $str = str_replace(' ', '',$str);
            		        $str = str_replace('<br/>', '',$str);
            		        preg_match_all('/Name\:\w*\,\w*\:(\#)*\d*/',$str,$matches);
            		        foreach ($matches as $v){
            		        	foreach ($v as $vv){
            		        		if($vv != '#' && $vv != ''){
            		        		    echo '<option value="'.$vv.'">'.$vv.'</option>';
            		        		}
            		        	}
            	        	}
                    	}
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-inline t-detail">
            <input type="text" name="weight" class="form-control inline" placeholder="重量">
            <input type="text" name="country" class="inline form-control" placeholder="国家"> 
            <input type="text" name="piece" class="inline form-control" placeholder="件数"> 
        </div>
    </form>
    <div id="countStock" class="form-inline"></div>
</div>
</div>

<div class="modal" style="display: block; padding-right: 17px;">
    <div class="modal-dialog">
        <div class="modal-content">
            <?php if($this->msg=='success'){ ?>
            <div class="modal-header">
                <h5 class="modal-title"><strong>保存分拣成功，请对分拣结果进行操作：</strong></h5>
            </div>
            <div class="modal-body success">
                <div class="form-group">订单来源：
                    <label for="site" class="control-label"><?php echo $this->site;?></label>
                </div>
                <div class="form-group">订单号码：
                    <label for="orderid" class="control-label"><?php echo $this->orderid;?></label>
                </div>
                <div class="text-center">
                    <a class="btn btn-primary packscan">进行打包扫描</a>
                    <a class="btn btn-primary operscan">进行操作扫描</a>
                </div>
            </div>
            <?php }else if($this->msg=='fail'){ ?>
            <div class="modal-header">
                <h5 class="modal-title"><strong>保存分拣失败，请重新进入分拣中心扫描，然后提交！</strong></h5>
            </div>
            <div class="modal-body">
                <div class="text-center">
                    <a class="btn btn-primary go-sort-scan">确定</a>
                </div>
            </div>
            <?php }else{ ?>
            <div class="modal-header">
                <h5 class="modal-title"><strong>请对分拣结果进行操作：</strong></h5>
            </div>
            <div class="modal-body success">
                <div class="form-group">订单来源：
                    <label for="site" class="control-label"><?php echo $this->site;?></label>
                </div>
                <div class="form-group">订单号码：
                    <label for="orderid" class="control-label"><?php echo $this->orderid;?></label>
                </div>
                <div class="text-center">
                    <a class="btn btn-primary packscan">进行打包扫描</a>
                    <a class="btn btn-primary operscan">进行操作扫描</a>
                </div>
            </div>
            <?php } ?>
        </div>
    </div>
</div>
<div class="modal-backdrop in"></div>

<script type="text/javascript">
$(document).ready(function(){
	$(".modal-content .success .packscan").click(function(){
		$(".breadcrumb .page-title").text('打包中心扫描');
		$(".oper-pack-time").text("打包时间");
		$("#oper-or-pack").val("进行打包");
		$(".modal").hide();
		$(".modal-backdrop").removeClass("modal-backdrop");
		$(".main").removeClass("hidden");
		$("input[name='barcode']").focus();//输入框获取焦点
		$("#addForm").addClass("add-pack");
	});
	$(".modal-content .success .operscan").click(function(){
		$(".breadcrumb .page-title").text('操作中心扫描');
		$(".oper-pack-time").text("操作时间");
		$("#oper-or-pack").val("进行操作");
		$(".modal").hide();
		$(".modal-backdrop").removeClass("modal-backdrop");
		$(".main").removeClass("hidden");
		$("#pack-memo").remove();
		$("input[name='barcode']").focus();//输入框获取焦点
		$("#addForm").addClass("add-oper");
		$("#addForm").attr("action","/log/oper");
		$("#billid").hide();
	});

	$("#scanForm").submit( function () {//扫描后异步生成条形码包含的信息内容插入到table中显示
		var barcode = $("input[name='barcode']").val();
		$("input[name='barcode']").val('');
		$("#scanForm .text-danger").text('');
		if(barcode == ''){
			alert('请扫描条形码或直接输入条形码条码');
		}else{
			$.get("/log/imloadstock", {code:barcode},
			    function(data){
			        $("#page-scan-info tbody").append(data.content);
			        checkScanRight($.trim(data.sku),$.trim(data.size));//检查扫描的条形码信息是否和给出的订单信息匹配
			        $("input[name='barcode']").focus();//输入框获取焦点
					countStock();
			    },'json'
		    );
		}
		return false;
	} );
	$("#addForm").submit( function () {
		var i = 0;
		var billid = $("input[name='billid']").val();
		$("input[name='isadd[]']").each(function (j) {
			if($(this).is(":checked")){
				i+=1;//如果被选中就将i加1
				/** 给input[name='isadd[]']进行排序，以方便提交到action好处理,即选中table表中的第几行，
				  * 就赋值到对应的input checkbox的值为第几，然后再将该值提交上去，从而可以判断其他一起提交上去的数组哪一个是被选中了的。
				  */
				$(this).val(j);//给input[name='isadd[]']进行排序，以方便提交到action好处理
			}
	    });
	    
	    if((billid == '') && $("#addForm").hasClass("add-pack")){
	        alert("请先填写快递单号！");
	        return false;
	    }
	    if(i>0){
	        return true;
	    }else{
	        alert("请先扫描，若已扫描则选择产品！");
	        return false;
	    }
	});
	//点击添加备注
	$("#pack-memo").toggle(
		function(){$(".textarea-memo").removeClass("hidden");},
		function(){$(".textarea-memo").addClass("hidden");}
	);
});
//删除分拣项
function deleteSort(id, obj) {
    if (confirm('确认删除?')) {
        $.ajax({
            type: "get",
            url: "/log/deletesort?id="+id,
            data: {},    
            success: function(data) {
                switch(data) {	
                    case "1":
                        $(obj).parent().parent().remove();
                        window.location.reload();
                        break;
        			default:alert('删除失败');
        		}
        	}			
        });
    }
}
function delscanlog(){
	$("input[name='isadd[]']").each(function () {
		//如果被选中
		if($(this).is(":checked")){
			$(this).parent('td').parent('tr').remove();//就删除
			countStock();
		}
    });
}
function checkScanRight(sku,size){
	var checkSku = false;//定义检查Sku结果初始值为false
	var checkSize = false;//定义检查Size结果初始值为false
	var checkNum = false;//定义检查尺码数量，默认为false
	var sizeAll = true;//定义是否检查了所有尺码，默认为true
	var size_num = parseInt($("input[name='onum_"+sku+"_"+size+"']").val());//该尺码总数量
	var check_num = parseInt($("input[name='cnum_"+sku+"_"+size+"']").val());//该尺码已检查数
	
	/**检查SKU是否匹配*/
	$("input[name='sort_sku[]']").each(function(i){
		if($(this).val()==sku){
			checkSku = true;
		}
	});
	
	/**检查尺码是否匹配*/
	$("input[name='o_size["+sku+"][]']").each(function(i){
		if($(this).val()==size){//检查指定SKU下的指定尺码是否匹配
			checkSize = true;
		}
	});
	
    if(isNaN(size_num)&&isNaN(check_num)){
        /**扫描得出的SKU或者尺码不对*/
    }else{//检查指定SKU下的指定尺码是否检查完
		$("input[name='cnum_"+sku+"_"+size+"']").val(check_num+1);
    	if(check_num+1 < size_num){
        	
    	}else{
    		checkNum = true;
    	}

    	/**遍历该SKU下的所有尺码，看看是否所有尺码都检查完了*/
    	$("input[name='o_size["+sku+"][]']").each(function(i){
    		var size_num = parseInt($(this).siblings(".size_num_"+$(this).val()).val());//该尺码总数量
    		var check_num = parseInt($(this).siblings(".check_num_"+$(this).val()).val());//该尺码已检查数
    		if(check_num < size_num){
    			sizeAll = false;
    		}
    	});
    }
    
	if(checkSize && checkSku){//扫描传入的SKU和尺码是否都正确
		repeatSku(sku,size);
		$("#page-scan-info tr:last").addClass("alert alert-success");		
	}else{
		$("#page-scan-info tr:last").addClass("alert alert-danger");
	}
}
/**处理有重复sku，重复size下的分拣项的高亮显示*/
function repeatSku(sku,size){
	var j=0;
	$("."+sku+size).each(function(i){
		if((!$(this).hasClass("alert-success")) && (j<1)){
			$(this).addClass("alert alert-success");
			j+=1;
		}
	});
}
//统计库存
function countStock(){
	/**扫描后页面滚动到底部*/
    var offset = $("#countStock").offset();
    if(offset.top > document.body.clientHeight/2){
        $("body,html").animate({"scrollTop": offset.top+"px"}, 500); 
    }
    var skus =[];
    $("#page-scan-info tbody tr").each(function(i){
        skus[i] = $(this).children("td").children("input[name='title[]']").val();
    });
    var sizes =[];
    $("#page-scan-info tbody tr").each(function(i){
        sizes[i] = $(this).children("td").children("input[name='size[]']").val();
    });

    var skusize = [];
    for(var i = 0;i<skus.length;i++){
        skusize[i] = "产品:"+skus[i]+' 尺码:'+sizes[i];
    }
    
    var res = [];
    for(var i = 0;i<skusize.length;i++){
        var c = 0;
    	for(var j = 0;j<skusize.length;j++){
        	if(skusize[i] == skusize[j]){
            	c++;
        	}
    	}
    	res.push(skusize[i]+"/"+c);
    }
    var arr = res.unique();
    //console.log(arr);
    var content = '';
    for(var k =0;k<arr.length;k++){
        content = content+"<p class='label label-success form-group'>"+arr[k]+"</p>";
    }
    $("#countStock").html(content);
}
Array.prototype.unique = function() {
	var ret = [];
	var o = {};
	for(var i=0, len=this.length; i<len; ++i){
		if(!o[this[i]]){ 
			ret.push(this[i]); 
			o[this[i]] = this[i]; 
		}
	}
	return ret; 
}
</script>
<script>
jQuery(document).ready(function(){
    
    "use strict";
    // Select2
    jQuery(".select2").select2({
      width: '100%'
    });
});
</script>